// pages/pie_chart/pie_chart.js
import * as echarts from '../../ec-canvas/echarts';
const app = getApp();
function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // 像素
  });
  canvas.setChart(chart);

  var option = {
    backgroundColor: '#ffffff',
    title:{
      text:'圆角环形图'
    },
    legend: {
      type: 'scroll',
      top: '8%',
      right: '1%',
      orient: 'vertical',
    },
    tooltip: {
      trigger: 'item'
    },
    series: (function (){
      var arr = [];
      var b = 0;
      var x = 170;
      while(b < app.globalData.data2.length){
        arr.push({
          name: b+1,
          type: 'pie',
          center: ['40%', x],
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: false,
            position: 'center'
          },
          data: (function (){
            var p = [];
            var c = 0;
            while(c < app.globalData.data2[b].length){
              p.push({
                value: app.globalData.data2[b][c], 
                name: app.globalData.data1[c]
              })
              c++;
            }
            return p;
          })(),
          labelLine: {
            show: false
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '40',
              fontWeight: 'bold'
            }
          }
        });
        b++;
        x = x + 270;
      }
      return arr;
    })()
  };
  chart.setOption(option);
  return chart;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      onInit: initChart
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  save_path(){
    setTimeout(() => {
      this.save();
    }, 1000);
  },

  save() {
    const ecComponent = this.selectComponent('#mychart-dom-bar');
    // 先保存图片到临时的本地文件，然后存入系统相册
    ecComponent.canvasToTempFilePath({
      success: res => {
        //console.log("tempFilePath:", res.tempFilePath)
        // 存入系统相册
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath || '',
          success: res => {
            //console.log("success", res)
            wx.showToast({
              title: '图片保存成功',
              icon: 'success'
            })
          },
          fail: res => {
            //console.log("fail", res)
            wx.showToast({
              title: '图片保存失败',
              icon: 'error'
            })
          }
        })
      },
      fail: res => console.log(res)
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})